﻿using Ice.Core;
using Ice.Lib.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CustomizationMaintEntry
{
    public class PubVariable
    {
        public static Boolean LoginEpicor()
        {
            try
            {
                Login(MyData.Login_ConfPath, MyData.Login_User, MyData.Login_Password, MyData.Login_CompanyID);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static Boolean LogoutEpicor()
        {
            try
            {
                MyData.Session.Dispose();
                MyData.Session = null;
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static void Login(string confPath, string user, string password, string company = "")
        {
            try
            {
                // string Url = @"D:\\Program Files\\Epicor Software\\ERP10.0Client\\Client\\Config\\default.sysconfig";
                Session _session = new Session(user, password, Session.LicenseType.DataCollection, confPath);
                if (company != "")
                {
                    _session.CompanyID = company;
                }
                //Session _sessionbak = _session;
                InitSession(_session);
                //_session.Dispose();
                //_session=null;
                //ILauncher IL = new ILauncher(_sessionbak);
                //Ice.Adapters.UD11Adapter UD11Adapter = new Ice.Adapters.UD11Adapter(IL);
                //UD11Adapter.BOConnect();

            }
            catch (Exception e1)
            {
                throw new Exception("Login Failed!" + e1.Message);
            }
        }

        public static void InitSession(Ice.Core.Session session)
        {
            if (session != null)
            {
                MyData.Session = session;
                MyData.IL = new ILauncher(MyData.Session);
                MyData.ServersPath = GetServersPath();
            }
        }

        public static string GetServersPath(string ServerName = null)
        {

            if (ServerName == null)
            {
                ServerName = GetServerName();
            }
            //return @"\\" + MyData.Server_IP + @"\wwwroot\" + ServerName + @"\Server";
            return @"\\" + MyData.Server_IP + @"\EpicorData";
        }

        public static string GetServerName()
        {
            if (MyData.Session == null)
            {
                throw new Exception("未加载session");
            }
            //MsgShow("", MyData.Session.AppServer.Substring(MyData.Session.AppServer.LastIndexOf('/') + 1));
            return MyData.Session.AppServer.Substring(MyData.Session.AppServer.LastIndexOf('/') + 1);
        }
    }
}
